<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      v-show="showSearch"
      @submit.native.prevent
    >
      <el-row :gutter="10">
        <el-col :span="4">
          <el-form-item
            prop="title"
          >
            <el-input
              v-model="queryParams.title"
              placeholder="请输入标题"
              clearable
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
        </el-col>
        <el-col :span="4">
          <el-form-item>
            <el-button
              type="primary"
              icon="el-icon-search"
              size="mini"
              @click="handleQuery"
            >搜索</el-button>
            <el-button
              icon="el-icon-refresh"
              size="mini"
              @click="resetQuery"
            >重置</el-button>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <el-row>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="registerList"
      @selection-change="handleSelectionChange"
      v-table
      height="0"
    >
      <el-table-column
        type="selection"
        width="55"
        align="center"
      />
      <!-- <el-table-column
        label="主键"
        align="center"
        prop="id"
      /> -->
      <el-table-column
        label="标题"
        align="center"
        prop="title"
        :show-overflow-tooltip="true"
      />
      <!-- <el-table-column
        label="发布状态"
        align="center"
        prop="isPublish"
      >
        <template slot-scope="scope">
          <span v-if="scope.row.isPublish == '0'">未发布</span>
          <span v-else>已发布</span>
        </template>
      </el-table-column> -->
      <el-table-column
        label="数据内容"
        align="center"
        prop="content"
        :show-overflow-tooltip="true"
      />
      <!-- <el-table-column
        label="备注"
        align="center"
        prop="remark"
        :show-overflow-tooltip="true"
      /> -->
      <el-table-column
        label="审核人"
        align="center"
        prop="examineUserName"
      />
      <el-table-column
        label="审核状态"
        align="center"
        prop="examineStatus"
      >
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.examine_status"
            :value="scope.row.examineStatus"
          />
        </template>
      </el-table-column>
      <el-table-column
        label="审核意见"
        align="center"
        prop="examineSuggestion"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        label="审核时间"
        align="center"
        prop="examineTime"
        width="180"
      >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.examineTime) }}</span>
        </template>
      </el-table-column>

      <!-- <el-table-column
        label="状态0隐藏1显示"
        align="center"
        prop="status"
      /> -->
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            v-if="scope.row.examineStatus == '1'"
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['compre:register:edit']"
          >审核</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改数据注册对话框 -->
    <el-dialog
      :title="title"
      :visible.sync="open"
      width="780px"
      append-to-body
      :close-on-click-modal="false"
    >
      <el-form
        ref="form"
        :model="form"
        :rules="rules"
        label-width="80px"
      >
        <el-row>
          <el-col :span="24">
            <el-form-item label="标题">
              <el-input
                v-model="form.title"
                placeholder="请输入标题"
                maxlength="20"
                disabled
              />
            </el-form-item>
          </el-col>
          <!-- <el-col :span="12">
            <el-form-item
              label="发布状态"
              prop="isPublish"
              style="height: 38px;"
            >
              <span slot="label">
                状态
              </span>
              <el-radio-group
                v-model="form.isPublish"
                disabled
              >
                <el-radio label="0">未发布</el-radio>
                <el-radio label="1">已发布</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col> -->
          <el-col :span="24">

            <el-form-item label="数据内容">
              <el-input
                v-model="form.content"
                type="textarea"
                placeholder="请输入数据内容"
                rows="3"
                disabled
              />
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item
              label="备注"
              prop="remark"
            >
              <el-input
                v-model="form.remark"
                type="textarea"
                placeholder="请输入备注"
                rows="3"
                disabled
              />
            </el-form-item>
          </el-col>

          <el-col :span="24">
            <el-form-item
              label="审核状态"
              prop="examineStatus"
              style="height: 38px;"
              label-width="100px"
            >
              <span slot="label">
                审核状态
              </span>
              <el-radio-group v-model="form.examineStatus">
                <el-radio label="2">通过</el-radio>
                <el-radio label="3">未通过</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item
              label="审核意见"
              prop="examineSuggestion"
            >
              <el-input
                type="textarea"
                v-model="form.examineSuggestion"
                placeholder="请输入审核意见"
                rows="3"
              />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div
        slot="footer"
        class="dialog-footer"
      >
        <el-button
          type="primary"
          @click="submitForm"
          icon="el-icon-check"
        >确 定</el-button>
        <el-button @click="cancel" icon="el-icon-close">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listRegister,
  getRegister,
  examineRegister,
} from "@/api/compre/register";

export default {
  name: "Register",
  dicts: ["examine_status"],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 数据注册表格数据
      registerList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        title: null,
      },
      // 表单参数
      form: {
        examineStatus: "2",
      },
      // 表单校验
      rules: {
        examineStatus: [
          { required: true, message: "请选择审核状态", trigger: "blur" },
        ],
        examineSuggestion: [
          { required: true, message: "审核意见不能为空", trigger: "blur" },
        ],
      },
      publishList: [
        {
          name: "未发布",
          value: "0",
        },
        {
          name: "已发布",
          value: "1",
        },
      ],
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询数据注册列表 */
    getList() {
      this.loading = true;
      listRegister(
        Object.assign({}, { isPublish: "1" }, this.queryParams)
      ).then((response) => {
        this.registerList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        title: null,
        content: null,
        remark: null,
        examineUser: null,
        examineStatus: "2",
        examineSuggestion: null,
        isPublish: "1",
        status: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加数据注册";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getRegister(id).then((response) => {
        this.form = response.data;
        this.form.examineStatus = "2";
        this.open = true;
        this.title = "数据注册审核";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            examineRegister(this.form).then((response) => {
              this.$modal.msgSuccess("审核成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm("是否确认删除该注册的数据项？")
        .then(function () {
          return delRegister(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "compre/register/export",
        {
          ...this.queryParams,
        },
        `register_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>
